<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Pertinence
 *
 * @author guillaume
 */
class Pertinence {

    protected $allSelectedDocuments;
    protected $selectedDocuments;
    protected $relevantDocuments;
    protected $matchedDocuments;
    protected $notes;
    protected $rappels;
    protected $question;

    public function __construct($question, array $allSelectedDocuments, array $relevantDocuments) {
        $this->question             = $question;
        $this->allSelectedDocuments = $allSelectedDocuments;
        $this->relevantDocuments    = $relevantDocuments;

        $this->matchedDocuments     = array();
        $this->selectedDocuments    = array();
        $this->notes                = array();
    }

    public function computeNotes($steps)
    {
        foreach($steps as $step)
        {
            //N premiers documents selectionnés
            $this->selectedDocuments[$step] = array_slice($this->allSelectedDocuments, 0, min($step, count($this->allSelectedDocuments)));

            //Liste des documents pertinents
            $qrelPaths = array_map(function($document)
            {
                return $document["path"];
            }, $this->relevantDocuments);

            //Intersection des documents pertinents et selectionnés
            $this->matchedDocuments[$step] = array_filter($this->selectedDocuments[$step], function(Document $document) use($qrelPaths)
            {
                return in_array($document->getPath(), $qrelPaths);
            });
            
            //Calcul de la précision finale
            $precision          = count($this->selectedDocuments[$step]) === 0 ? 0 : count($this->matchedDocuments[$step]) / count($this->selectedDocuments[$step]);
            
            //Calcul du rappel
            $rappel             = count($this->relevantDocuments) === 0 ? 0 : count($this->matchedDocuments[$step]) / count($this->relevantDocuments);
            
            $this->notes[$step]  = array("precision" => $precision, "rappel" => $rappel);
        }

        return $this;
    }
    
    public function getSelectedDocuments($step) {
        return $this->selectedDocuments[$step];
    }
    
    public function getNote($step) {
        return $this->notes[$step];
    }

    public function getMatchedDocuments($step) {
        return $this->matchedDocuments[$step];
    }

    public function getNotes() {
        return $this->notes;
    }


    public function getDbDocuments() {
        return $this->selectedDocuments;
    }

    public function getRelevantDocuments() {
        return $this->relevantDocuments;
    }

    public function getQuestion() {
        return $this->question;
    }
}
